part of '../index.dart';

class MessageGiftGrid extends StatefulWidget {
  const MessageGiftGrid(
    this.wrapper, {
    super.key,
    required this.gifts,
  });
  final ImGitfWrapperModel wrapper;
  final List<ImGitfModel> gifts;

  @override
  State<MessageGiftGrid> createState() => _MessageGiftGridState();
}

class _MessageGiftGridState extends State<MessageGiftGrid> {
  ImGitfModel? active;

  Widget _option(BuildContext context, ImGitfModel model) {
    final isActive = active == model;
    return GestureDetector(
      onTap: () {
        setState(() => active = model);
        context.read<ChatProvider>().gift = model;
      },
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          border: Border.all(
            color: isActive ? const Color(0xFF8A72FE) : Colors.white,
          ),
        ),
        child: Column(
          children: [
            Expanded(child: image(model.objectUrl ?? '')),
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(Images.icChatDiamond, width: 15, height: 15),
                Padding(
                  padding: const EdgeInsets.only(bottom: 2),
                  child: Text(
                    '${model.price ?? 999}',
                    style: const TextStyle(
                      color: Color(0xFF1B1B1B),
                      fontWeight: FontWeight.w500,
                      fontSize: 14,
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      padding: const EdgeInsets.only(bottom: 70),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 4,
        mainAxisSpacing: 5,
        crossAxisSpacing: 5,
        childAspectRatio: 0.85,
      ),
      itemCount: widget.gifts.length,
      itemBuilder: (context, index) => _option(context, widget.gifts[index]),
    );
  }
}
